package com.yuncheng.spcyApi.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.yuncheng.spcyApi.entity.SpcyRsCity;
import com.yuncheng.spcyApi.vo.common.HttpCityVo;


import java.util.ArrayList;
import java.util.List;

/**
 * @author hua
 */
public class CityUtils {

    public static void main(String[] args) throws Exception {
        List<SpcyRsCity> cityList = getAllProvinceCityArea();
        System.out.println(cityList.size());
    }

    private final static String City_Root_Api = "https://www.mxnzp.com/api/address";
    private final static String LIST = "/list"; // 全国城市列表
    private final static String search = "/search"; // 搜索全国城市列表信息 type:0-查询省份 1-查询城市 value 被查询的省份或者城市名称
    private final static String province = "/v3/list/province"; // 获取全国城市信息-省份列表
    private final static String city = "/v3/list/city"; //  获取全国城市信息-城市列表  provinceCode 省份编码
    private final static String area = "/v3/list/area"; //  获取全国城市信息-区域列表  provinceCode 省份编码 cityCode	市编码
    private final static String street = "/v3/list/street"; //  获取全国城市信息-街道列表  provinceCode 省份编码 cityCode	市编码 areaCode 区编码
    private final static String village = "/v3/list/village"; //  获取全国城市信息-村委会列表  provinceCode 省份编码 cityCode	市编码 areaCode 区编码 streetCode 街道编码

    private final static String APP_ID = "app_id=nqglekhfsqj7lvsp";
    private final static String APP_SECRET = "app_secret=nhIvgXdo56lX0wM0Ziy2U7OHmckkEdDj";

    public static List<SpcyRsCity> getAllProvinceCityArea() throws Exception {

        List<SpcyRsCity> cityDataList = new ArrayList<>();

        String url = City_Root_Api + LIST;
        url += "?" + APP_ID;
        url += "&" + APP_SECRET;
        String result = HttpServletUtils.doGet(url);

        JSONObject jsonObject = JSONObject.parseObject(result);
        String code = jsonObject.getString("code");
        if (code.equals("1")){
            List<HttpCityVo> list = JSONObject.parseArray(jsonObject.get("data").toString(), HttpCityVo.class);


            int order = 0;
            // 省
            for (HttpCityVo cityVo : list){
                order++;
//                System.out.println("code1: "+cityVo.getCode());
//                System.out.println("name1: "+cityVo.getName());

                SpcyRsCity province = new SpcyRsCity();
                String provinceid = GetUuIdUtils.ReplaceUuId();
                province.setForder(order);
                province.setId(provinceid);
                province.setProvincebh(cityVo.getCode());
                province.setProvince(cityVo.getName());

                cityDataList.add(province);

                if (CollectionUtil.isNotEmpty(cityVo.getPchilds())) {

                    // 市
                    for (HttpCityVo cityVo1 : cityVo.getPchilds()) {
                        order++;
//                        System.out.println("code2: "+cityVo1.getCode());
//                        System.out.println("name2: "+cityVo1.getName());

                        SpcyRsCity city = new SpcyRsCity();
                        String cityid = GetUuIdUtils.ReplaceUuId();
                        city.setForder(order);
                        city.setId(cityid);
                        city.setProvinceid(provinceid);
                        city.setCitybh(cityVo1.getCode());
                        city.setCity(cityVo1.getName());
                        city.setProvincecity(province.getProvince() + city.getCity());

                        cityDataList.add(city);

                        if (CollectionUtil.isNotEmpty(cityVo1.getCchilds())) {
                            // 区
                            for (HttpCityVo cityVo2 : cityVo1.getCchilds()) {
                                order++;
//                                System.out.println("code3: " + cityVo2.getCode());
//                                System.out.println("name3: " + cityVo2.getName());

                                SpcyRsCity area = new SpcyRsCity();
                                String areaid = GetUuIdUtils.ReplaceUuId();
                                area.setForder(order);
                                area.setId(areaid);
                                area.setCityid(cityid);

                                area.setAreabh(cityVo2.getCode());
                                area.setArea(cityVo2.getName());

                                area.setProvincecity(city.getProvincecity());
                                area.setProvincecityarea(city.getProvincecity() + area.getArea());

                                cityDataList.add(area);
                            }
                        }
                    }
                }
            }
        }else {
            String msg = jsonObject.getString("msg");
            System.out.println(msg);
        }

        return cityDataList;
    }


}
